{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7a0b28cf",
   "metadata": {},
   "source": [
    "# 1.语言录制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "e0ecb801",
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "76365a32",
   "metadata": {},
   "source": [
    "# 2. 语言识别（百度的ASR）auto speech recognition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "80c1e357",
   "metadata": {},
   "outputs": [],
   "source": [
    "import bdasr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "0aee3f91",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Request time cost 0.816876\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'网络与新媒体专业。'"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bdasr_result  = eval(bdasr.asr('1.wav'))['result'][0]\n",
    "bdasr_result"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f788b19",
   "metadata": {},
   "source": [
    "# 3. 连接字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "9dcf579d",
   "metadata": {},
   "outputs": [],
   "source": [
    "qa = {\n",
    "    \"网络与新媒体专业。\":\"网络与新媒体专业是一门普通高等学校本科专业,属新闻传播学类专业,学制四年,授予文学学士学位\",\n",
    "    \"你是谁？\":\"我是集美貌和智慧于一体的智能小网同学\",\n",
    "    '你好。':\"你也好，今天听起来心情不错哦\",\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "f50b2e44",
   "metadata": {},
   "outputs": [],
   "source": [
    "import opchat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "8a91296a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'网络与新媒体专业是一门普通高等学校本科专业,属新闻传播学类专业,学制四年,授予文学学士学位'"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result = qa.get(bdasr_result)\n",
    "result"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "641e982c",
   "metadata": {},
   "source": [
    "# 4.语言合成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "a42fb1a8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import bdtts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "a273e17e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SUCCESS WITH TOKEN: 24.e18310fe4f279ac277ae244177a4a989.2592000.1687314360.282335-33296560 ; EXPIRES IN SECONDS: 2592000\n",
      "test on Web Browserhttp://tsn.baidu.com/text2audio?tok=24.e18310fe4f279ac277ae244177a4a989.2592000.1687314360.282335-33296560&tex=%25E7%25BD%2591%25E7%25BB%259C%25E4%25B8%258E%25E6%2596%25B0%25E5%25AA%2592%25E4%25BD%2593%25E4%25B8%2593%25E4%25B8%259A%25E6%2598%25AF%25E4%25B8%2580%25E9%2597%25A8%25E6%2599%25AE%25E9%2580%259A%25E9%25AB%2598%25E7%25AD%2589%25E5%25AD%25A6%25E6%25A0%25A1%25E6%259C%25AC%25E7%25A7%2591%25E4%25B8%2593%25E4%25B8%259A%252C%25E5%25B1%259E%25E6%2596%25B0%25E9%2597%25BB%25E4%25BC%25A0%25E6%2592%25AD%25E5%25AD%25A6%25E7%25B1%25BB%25E4%25B8%2593%25E4%25B8%259A%252C%25E5%25AD%25A6%25E5%2588%25B6%25E5%259B%259B%25E5%25B9%25B4%252C%25E6%258E%2588%25E4%25BA%2588%25E6%2596%2587%25E5%25AD%25A6%25E5%25AD%25A6%25E5%25A3%25AB%25E5%25AD%25A6%25E4%25BD%258D&per=4&spd=5&pit=5&vol=5&aue=6&cuid=123456PYTHON&lan=zh&ctp=1\n",
      "result saved as :result.wav\n"
     ]
    }
   ],
   "source": [
    "bdtts.tts(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "33515559",
   "metadata": {},
   "source": [
    "# 5. 播放音频"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "41756e45",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pydub import AudioSegment\n",
    "from pydub.playback import play\n",
    "song = AudioSegment.from_wav('result.wav')\n",
    "play(song)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7b0d0824",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
